package cn.woochen.comlib

import androidx.lifecycle.ViewModel
import cn.woochen.comlib.config.EventLiveData
import com.google.gson.GsonBuilder
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import okhttp3.MediaType
import okhttp3.RequestBody

/**
 * viewmodel基类
 *
 */
abstract class BaseViewModel : ViewModel() {
    val loadingChange: UiLoadingChange by lazy { UiLoadingChange() }

    /**
     * 可通知Activity/fragment 显示隐藏加载框
     */
    inner class UiLoadingChange {

        //显示加载框
        val showDialog by lazy { MutableSharedFlow<String>() }

        //隐藏
        val dismissDialog by lazy { MutableSharedFlow<String>() }
    }

    fun getRequestBody(map: MutableMap<String, Any>? = null): RequestBody {
        val finalMap: MutableMap<String, Any>?
        if (map == null) {
            finalMap = mutableMapOf()
        } else finalMap = map
        val toJson = GsonBuilder().disableHtmlEscaping().create().toJson(finalMap)
        return RequestBody.create(MediaType.parse("application/json; charset=utf-8"), toJson)
    }
}